package com.spring.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.spring.domain.Account;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@Configuration
@ComponentScan({"com.spring.service","com.spring.log"})
@EnableAspectJAutoProxy
@EnableTransactionManagement
@Import(DBConfig.class)
public class SpringConfig {
    //配置SqlSessionFactoryBean

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //配置datasource
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置包别名
        sqlSessionFactoryBean.setTypeAliasesPackage("com.spring.domain");
        return  sqlSessionFactoryBean;
    }

//扫描Mapper
    @Bean
    public MapperScannerConfigurer mapperScannerConfig(){
        MapperScannerConfigurer mapperScannerConfig = new MapperScannerConfigurer();
        //设置mapper所在的包
        mapperScannerConfig.setBasePackage("com.spring.mapper");
        return mapperScannerConfig;
    }
    //设置事务管理器
    @Bean
    public TransactionManager transactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

}
